package com.xialing.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xialing.weblog.common.domain.dos.VideoCollectionRelDO;

import java.util.List;

/**
 * @author: 夏翎
 * @url: www.xialing.com
 * @date: 2025-09-23
 * @description: 合集视频关联Mapper
 **/
public interface VideoCollectionRelMapper extends BaseMapper<VideoCollectionRelDO> {

    /**
     * 根据合集 ID 查询关联的视频 ID 集合（按集数排序）
     * @param collectionId 合集ID
     * @return 视频ID列表
     */
    default List<Long> selectVideoIdsByCollectionId(Long collectionId) {
        // 构建查询条件
        LambdaQueryWrapper<VideoCollectionRelDO> wrapper = Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .select(VideoCollectionRelDO::getVideoId)
                .eq(VideoCollectionRelDO::getCollectionId, collectionId)
                .orderByAsc(VideoCollectionRelDO::getEpisodeNumber); // 按集数升序

        return selectList(wrapper).stream()
                .map(VideoCollectionRelDO::getVideoId)
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 根据合集 ID 查询所有关联记录（按集数排序）
     * @param collectionId 合集ID
     * @return 关联记录列表
     */
    default List<VideoCollectionRelDO> selectByCollectionId(Long collectionId) {
        return selectList(Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .eq(VideoCollectionRelDO::getCollectionId, collectionId)
                .orderByAsc(VideoCollectionRelDO::getEpisodeNumber)); // 按集数升序
    }

    /**
     * 根据视频 ID 查询关联的合集 ID 集合
     * @param videoId 视频ID
     * @return 合集ID列表
     */
    default List<Long> selectCollectionIdsByVideoId(Long videoId) {
        // 构建查询条件
        LambdaQueryWrapper<VideoCollectionRelDO> wrapper = Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .select(VideoCollectionRelDO::getCollectionId)
                .eq(VideoCollectionRelDO::getVideoId, videoId);

        return selectList(wrapper).stream()
                .map(VideoCollectionRelDO::getCollectionId)
                .collect(java.util.stream.Collectors.toList());
    }

    /**
     * 根据合集 ID 删除关联记录
     * @param collectionId 合集ID
     * @return 删除结果
     */
    default int deleteByCollectionId(Long collectionId) {
        return delete(Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .eq(VideoCollectionRelDO::getCollectionId, collectionId));
    }

    /**
     * 根据视频 ID 删除关联记录
     * @param videoId 视频ID
     * @return 删除结果
     */
    default int deleteByVideoId(Long videoId) {
        return delete(Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .eq(VideoCollectionRelDO::getVideoId, videoId));
    }

    /**
     * 根据合集 ID 和视频 ID 删除关联记录
     * @param collectionId 合集ID
     * @param videoId 视频ID
     * @return 删除结果
     */
    default int deleteByCollectionIdAndVideoId(Long collectionId, Long videoId) {
        return delete(Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .eq(VideoCollectionRelDO::getCollectionId, collectionId)
                .eq(VideoCollectionRelDO::getVideoId, videoId));
    }

    /**
     * 根据合集 ID 统计关联的视频数量
     * @param collectionId 合集ID
     * @return 视频数量
     */
    default Long countByCollectionId(Long collectionId) {
        return selectCount(Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .eq(VideoCollectionRelDO::getCollectionId, collectionId));
    }

    /**
     * 查询合集中下一个集数
     * @param collectionId 合集ID
     * @return 下一个集数
     */
    default Integer selectNextEpisodeNumber(Long collectionId) {
        VideoCollectionRelDO maxEpisode = selectOne(Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .select(VideoCollectionRelDO::getEpisodeNumber)
                .eq(VideoCollectionRelDO::getCollectionId, collectionId)
                .orderByDesc(VideoCollectionRelDO::getEpisodeNumber)
                .last("LIMIT 1"));
        
        return maxEpisode != null ? maxEpisode.getEpisodeNumber() + 1 : 1;
    }

    /**
     * 检查合集和视频的关联是否存在
     * @param collectionId 合集ID
     * @param videoId 视频ID
     * @return 是否存在
     */
    default boolean existsByCollectionIdAndVideoId(Long collectionId, Long videoId) {
        return selectCount(Wrappers.<VideoCollectionRelDO>lambdaQuery()
                .eq(VideoCollectionRelDO::getCollectionId, collectionId)
                .eq(VideoCollectionRelDO::getVideoId, videoId)) > 0;
    }
}

